package com.bkhech.home.practice.socket.websocket.JavaWebsocket;

import cn.hutool.core.util.RandomUtil;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.enums.ReadyState;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ServerHandshake;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

/**
 * 客户端
 * @author guowm
 * @date 2023/9/26
 */
public class ChatClient extends WebSocketClient {

    /**
     *
     * @param serverUri new URI("ws://127.0.0.1:8887/")
     */
    public ChatClient(URI serverUri) {
        super(serverUri);
        System.out.println("ChatClient Started!");
    }

    /**
     * 连接成功的回调
     * @param shs The handshake of the websocket instance
     */
    @Override
    public void onOpen(ServerHandshake shs) {
        System.out.println("onOpen: " + shs.getHttpStatusMessage());
    }

    /**
     * 收到来自服务端的消息 字符串
     * @param message The UTF-8 decoded message that was received.
     */
    @Override
    public void onMessage(String message) {
        System.out.println(message);
    }

    /**
     * 收到来自服务端的消息 字节数组
     * @param bytes The binary message that was received.
     */
    @Override
    public void onMessage(ByteBuffer bytes) {
        super.onMessage(bytes);
        System.out.println("ByteBuffer = " + bytes.toString());
    }

    /**
     * 连接关闭的回调
     * @param code   The codes can be looked up here: {@link CloseFrame}
     * @param reason Additional information string
     * @param remote Returns whether or not the closing of the connection was initiated by the remote
     *               host.
     */
    @Override
    public void onClose(int code, String reason, boolean remote) {
        System.out.println("onClose: code = " + code
                + ", reason = " + reason
                + ", remote = " + remote);
    }

    @Override
    public void onError(Exception ex) {
        System.out.println("onError: " + ex.toString());
    }

    public static void main(String[] args) throws IOException {
        try {
            final ChatClient client = new ChatClient(new URI("ws://127.0.0.1:8887/" + RandomUtil.randomInt(100)));
            client.connectBlocking(10, TimeUnit.SECONDS);

            while (!client.getReadyState().equals(ReadyState.OPEN)) {
                System.out.println("连接中...");
                Thread.sleep(1000);
            }
            BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                String in = sysin.readLine();
                if (in.equals("exit")) {
                    client.close();
                    break;
                }
                client.send(in);
            }
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
